/* Examples of speech recognition with multiple keywords.

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "board.h"
#include "esp_system.h"

#include "nvs_flash.h"
#include "nvs.h"
#include "irTask.h"
#include "buttonTask.h"

#include "bluetooth.h"
#include "mywifi.h"

#include "myhttp.h"
#include "player.h"
#include "asrtask.h"
#include "dht11.h"
#include "clock.h"
#include "driver/gpio.h"

static const char *TAG = "MAIN APP";

#define USE_HEAP_MANGER 1
/*----任务配置----*/
#define ASR_TASK_PRO 6
#define BUTTON_TASK_PRO 4
#define IR_TX_TASK_PRO 5
#define IR_RX_TASK_PRO 6
#define HTTP_GET_WEATHER_TASK_PRO 10
#define HEAP_MANAGER_TASK_PRO 20

#define ASR_TASK_SIZE 5120
#define BUTTON_TASK_SIZE 4096
#define IR_RX_TASK_SIZE 2048
#define IR_TX_TASK_SIZE 2048
#define HTTP_GET_WEATHER_TASK_SIZE 4096
#define HEAP_MANAGER_TASK_SIZE 2048

#if USE_HEAP_MANGER
/* 
 * 堆栈管理任务
 * 打印各个任务的堆栈使用情况
 */
void heap_manager_task(void *agr)
{
    const char *TAG = "heap_manger";

    esp_log_level_set(TAG, ESP_LOG_INFO);

    while (1)
    {
        vTaskDelay(10000 / portTICK_RATE_MS); //10s

        //printf("---heap manger---\r\n");
        //打印任务的堆栈情况
        //printf("%-10.10s,free heap:%d\r\n",pcTaskGetTaskName(buttonTask_handle),uxTaskGetStackHighWaterMark(buttonTask_handle));
        //printf("%-10.10s,free heap:%d\r\n",pcTaskGetTaskName(ir_tx_handle),uxTaskGetStackHighWaterMark(ir_tx_handle));
        //printf("%-10.10s,free heap:%d\r\n",pcTaskGetTaskName(ir_rx_handle),uxTaskGetStackHighWaterMark(ir_rx_handle));
        //printf("%-10.10s,free heap:%d\r\n",pcTaskGetTaskName(ASR_task_handle),uxTaskGetStackHighWaterMark(ASR_task_handle));

        //打印全局时间
        printf("20%d-%d-%d %d:%d:%d\r\n", global_clk.cal.year, global_clk.cal.month, global_clk.cal.date, global_clk.cal.hour, global_clk.cal.minute, global_clk.cal.second);

        //printf("---heap manger---\r\n");
    }
}
#endif
/*
 * 外设初始化
 */
audio_event_iface_handle_t periph_init()
{

    ESP_LOGI(TAG, "[ buttonTask ] Initialize peripherals");

    esp_periph_config_t periph_cfg = DEFAULT_ESP_PERIPH_SET_CONFIG();
    esp_periph_set_handle_t set = esp_periph_set_init(&periph_cfg); //初始化空的外设集合

    //初始化按键外设
    ESP_LOGI(TAG, "[1.1] Initialize keys on board");
    audio_board_key_init(set);

    ESP_LOGI(TAG, "[ 1.2 ] Set up  event listener");
    audio_event_iface_cfg_t evt_cfg = AUDIO_EVENT_IFACE_DEFAULT_CFG();
    audio_event_iface_handle_t evt = audio_event_iface_init(&evt_cfg); //初始化音频事件接口

    //监听按键的事件
    ESP_LOGI(TAG, "[1.3] Listening event from peripherals");
    audio_event_iface_handle_t periph_evt_if = esp_periph_set_get_event_iface(set); //获取外设事件接口
    audio_event_iface_set_listener(periph_evt_if, evt);                             //使用evt监听periph_evt_if
    return evt;
}
void app_main()
{

    esp_log_level_set("*", ESP_LOG_WARN);
    esp_log_level_set(TAG, ESP_LOG_INFO);

    /*--------------初始化存储系统--------------*/

    storage_init();

    /*--------------初始化外设--------------*/

    //audio_event_iface_handle_t event = periph_init();

    xTaskCreate(buttonTask, "button_task", BUTTON_TASK_SIZE, NULL, BUTTON_TASK_PRO, &buttonTask_handle);

    //all_gpio_init();

    wifi_init_sta(); //wifi初始化

    ble_init();

    IR_init(); //红外功能初始化

    player_init(); //播放器初始化

    /*--------------创建任务-------------*/

    

    xTaskCreate(rmt_ir_txTask, "ir_tx", IR_TX_TASK_SIZE, NULL, IR_TX_TASK_PRO, &ir_tx_handle);
    xTaskCreate(rmt_ir_rxTask, "ir_rx", IR_RX_TASK_SIZE, NULL, IR_RX_TASK_PRO, &ir_rx_handle);
    xTaskCreate(ASR_Task, "ASR_Task", ASR_TASK_SIZE, NULL, ASR_TASK_PRO, NULL);
    xTaskCreate(clock_task, "clock_Task", IR_TX_TASK_SIZE, NULL, IR_TX_TASK_PRO, NULL); //不完善
    httptask_init();
#if USE_HEAP_MANGER
    xTaskCreate(heap_manager_task, "heap_manager_task", HEAP_MANAGER_TASK_SIZE, NULL, HEAP_MANAGER_TASK_PRO, NULL);
#endif

    while (1)
    {

        vTaskDelay(1000 / portTICK_RATE_MS);
    }

    return;
}
